package com.xgkj.diyiketang.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.VideoLiveVAdapter;
import com.xgkj.diyiketang.data.VideoInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.GetSellerVideoListTask;
import com.xgkj.diyiketang.task.MoreVideoListTask;
import com.xgkj.diyiketang.task.SearchVideoTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;
import com.xgkj.diyiketang.yizhibo.Constants;

import java.util.ArrayList;
import java.util.List;


public class VideoLiveVFragment extends BaseFragment {
    private View cv = null;
    private Context mContext;
    private PullToRefreshListView mListView;
    private VideoLiveVAdapter mAdapter;
    private int moreVideoTaskId = -1;
    private int searchTaskId = -1;

    private int moreSellerListTaskId = -1;
    private int size = 10;
    private int currentPage = 1;
    private String typeId;
    private String keyword;
    private String from;
    private String businessId;
    private NoDataUtil mNoData;


    @Override
    protected int getContentViewId() {
        return 0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        cv = inflater.inflate(R.layout.fragment_video_live_v, null);
        mContext = getActivity();
        initView();
        Intent intent = getActivity().getIntent();
        from = intent.getStringExtra(ProtocolConst.MORE_FROM);
        if(from != null && from.equals(ProtocolConst.FORM_SEARCH)){
            keyword = intent.getStringExtra("keyword");
            currentPage = 1;
            doSearch();//0a7a929a76ea44829672fe63733a5b42
        }else if(from != null && from.equals(ProtocolConst.FORM_FIRST)) {
            typeId = intent.getStringExtra("TYPEID");
            PalLog.e("更多传过来的id",typeId);
            currentPage = 1;
            getMoreVieoList();
        }else if(from != null && from.equals(ProtocolConst.FORM_SELLER)){
            businessId = intent.getStringExtra("BUSINESSID");
            currentPage = 1;
            getSellerVideoList();
        }
        RelativeLayout contentLayout = (RelativeLayout) findViewById(R.id.content_layout);
        mNoData = new NoDataUtil(mContext,contentLayout);
        return cv;
    }


    private View findViewById(int viewid) {
        return cv.findViewById(viewid);
    }


    private void doSearch(){
        if(currentPage == 0){
            mListView.onRefreshComplete();
            //  ToastUtil.showMessage(mContext,R.string.no_more);
            return;
        }
        SearchVideoTask task = new SearchVideoTask(keyword,20,currentPage);
        task.addListener(new MoreListner(mContext));
        searchTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private void  getMoreVieoList(){
        if(currentPage == 0){
            mListView.onRefreshComplete();
            // ToastUtil.showMessage(mContext, R.string.no_more);
            return;
        }
        if(moreVideoTaskId != -1){
            return;
        }
        MoreVideoListTask task = new MoreVideoListTask(size,currentPage,typeId,null,null,null);
        task.addListener(new MoreListner(mContext));
        moreVideoTaskId = getEatShowApp().getTaskManager().addTask(task);

    }

    private void  getSellerVideoList(){
        if(currentPage == 0){
            mListView.onRefreshComplete();
            // ToastUtil.showMessage(mContext, R.string.no_more);
            return;
        }
        if(moreVideoTaskId != -1){
            return;
        }
        GetSellerVideoListTask svTask = new GetSellerVideoListTask(size,currentPage,businessId);
        svTask.addListener(new MoreListner(mContext));
        moreSellerListTaskId = getEatShowApp().getTaskManager().addTask(svTask);

    }

    class MoreListner extends ShowToastTaskEventListener {

        public MoreListner(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == moreVideoTaskId){
                moreVideoTaskId = -1;
                mListView.onRefreshComplete();
                if (currentPage == 1){
                    mAdapter.clearData();
                }
                List<VideoInfo> attentionList = new ArrayList<VideoInfo>();
                if(data != null){
                    attentionList = (List<VideoInfo>) data;
                }

                if(attentionList != null && attentionList.size()>0){
                    mAdapter.updateData(attentionList);
                }

                currentPage = result;
                if (mAdapter != null && mAdapter.getCount()== 0){
                    mNoData.ListViewEmpty(mListView,null,null,-1);
                }else {
                    mNoData.ListViewNoEmpty(mListView);
                }
            }else if(taskId == searchTaskId){
                List<VideoInfo> searchList = new ArrayList<VideoInfo>();
                if(data != null){
                    mListView.onRefreshComplete();
                    if (currentPage == 1){
                        mAdapter.clearData();
                    }
                    searchList = (List<VideoInfo>) data;
                    if(searchList != null && searchList.size() > 0) {
                        mAdapter.clearData();
                        mAdapter.updateData(searchList);
                    }
                    currentPage = result;
                    if (mAdapter != null && mAdapter.getCount()== 0){
                        mNoData.ListViewEmpty(mListView,null,null,-1);
                    }else {
                        mNoData.ListViewNoEmpty(mListView);
                    }
                }
            }else if(taskId == moreSellerListTaskId){
                moreSellerListTaskId = -1;
                mListView.onRefreshComplete();
                if (currentPage == 1){
                    mAdapter.clearData();
                }
                List<VideoInfo> attentionList = new ArrayList<VideoInfo>();
                if(data != null){
                    attentionList = (List<VideoInfo>) data;
                }

                if(attentionList != null && attentionList.size()>0){
                    mAdapter.updateData(attentionList);
                }

                currentPage = result;
                if (mAdapter != null && mAdapter.getCount()== 0){
                    mNoData.ListViewEmpty(mListView,null,null,-1);
                }else {
                    mNoData.ListViewNoEmpty(mListView);
                }
            }

        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if(taskId == moreVideoTaskId){
                moreVideoTaskId = -1;
                mListView.onRefreshComplete();
            }else if(taskId == searchTaskId){
                searchTaskId = -1;
                mListView.onRefreshComplete();
            }else if(taskId == moreSellerListTaskId){
                moreSellerListTaskId=-1;
                mListView.onRefreshComplete();
            }
        }
    }
    private void initView() {
        mListView = (PullToRefreshListView) findViewById(R.id.more_list);
        mListView.init(mContext, true);
        mAdapter = new VideoLiveVAdapter(mContext);
        mListView.setAdapter(mAdapter);
        mListView.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                if(from != null && from.equals(ProtocolConst.FORM_SEARCH)){
                    doSearch();
                }else if(from != null && from.equals(ProtocolConst.FORM_FIRST)){
                    getMoreVieoList();
                }else if(from != null && from.equals(ProtocolConst.FORM_SELLER)){
                    getSellerVideoList();
                }
            }

            });
        mListView.setOnLoadDataListener(new PullToRefreshListView.OnLoadDataListener() {
            @Override
            public void onPullRefresh() {

            }

            @Override
            public void onLoadMore() {
                if(from != null && from.equals(ProtocolConst.FORM_SEARCH)){
                    doSearch();
                }else if(from != null && from.equals(ProtocolConst.FORM_FIRST)){
                    getMoreVieoList();
                }else if(from != null && from.equals(ProtocolConst.FORM_SELLER)){
                    getSellerVideoList();
                }
            }

        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!TextUtils.isEmpty(EatShowApp.getACache().getAsString(Constants.USE_GUANKANQUAN))){
            String isUse = EatShowApp.getACache().getAsString(Constants.USE_GUANKANQUAN);
            if (isUse.equals("true")){
                getMoreVieoList();
            }
        }

    }
}
